package com.futures.questionbank.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.futures.questionbank.R;
import com.futures.questionbank.Utils.UItils;
import com.futures.questionbank.base.BaseFragment;
import com.futures.questionbank.db.table.chapter;
import com.futures.questionbank.db.table.zixun;
import com.futures.questionbank.my.WrongActivity;
import com.futures.questionbank.ui.home.CollectActivity;
import com.futures.questionbank.ui.home.InfoActivity;
import com.futures.questionbank.ui.home.InfoActivity2;
import com.futures.questionbank.ui.home.InfoActivity3;
import com.futures.questionbank.ui.home.LianXiActivity;
import com.futures.questionbank.ui.home.OneHotActivity;
import com.futures.questionbank.ui.home.SecordActivity;
import com.futures.questionbank.ui.home.ZhangJiActivity;
import com.futures.questionbank.ui.home.ZhengTiActivity;
import com.futures.questionbank.view.MyImageLoader;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.Transformer;

/**
 * author : huang
 * date   : 2020/6/15 0015 16:05
 * version: 1.0
 */
public class HomeFragment extends BaseFragment implements View.OnClickListener {
    private RecyclerView rv;
    private RelativeLayout textView,textView2,textView3;
    private List<chapter> users;
    private Banner mBanner;
    private List<String> list_path = new ArrayList<>();
    private List<String> list_path2 = new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.home_fragment;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        textView =  rootView.findViewById(R.id.rl_1);
        textView2 =  rootView.findViewById(R.id.rl_2);
        textView3 =  rootView.findViewById(R.id.rl_3);
        mBanner = rootView.findViewById(R.id.bannerContainer);
        LinearLayout ll_fabu = rootView.findViewById(R.id.ll_fabu);
        LinearLayout ll_hot = rootView.findViewById(R.id.ll_hot);
        LinearLayout ll_new = rootView.findViewById(R.id.ll_new);
        ll_fabu.setOnClickListener(this);
        ll_hot.setOnClickListener(this);
        ll_new.setOnClickListener(this);
        initView();
    }

    private void initView() {
        UItils.showDialog(getActivity());
        List<zixun> zixuns = SQLite.select().
                from(zixun.class).queryList();
        list_path.clear();
        list_path2.clear();
        for (zixun zixun : zixuns) {
            if (!TextUtils.isEmpty(zixun.getIcon())){
                list_path.add(zixun.getIcon());
                list_path2.add(zixun.getDesc());
            }
        }

        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        mBanner.setImageLoader(new MyImageLoader());
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
       // mBanner.setBannerAnimation(Transformer.Default);
        //  mBanner.setBannerTitles(list_title);
        //设置轮播间隔时间
        mBanner.setDelayTime(3000);
        //设置是否为自动轮播，默认是true
        mBanner.isAutoPlay(true);
        //设置指示器的位置，小点点，居中显示
        mBanner.setIndicatorGravity(BannerConfig.CENTER);

        mBanner.setImages(list_path).start();


        //跳转到url
        mBanner.setOnBannerListener(position -> {
            String url = list_path2.get(position);
            Intent intent = new Intent();
            intent.putExtra("desc", url);
            intent.putExtra("img", list_path.get(position));
            intent.setClass(getActivity(), InfoActivity3.class);
            getActivity().startActivity(intent);
        });



        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UItils.startActivity(OneHotActivity.class);
            }
        });

        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UItils.startActivity(ZhengTiActivity.class);
            }
        });
        textView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UItils.startActivity(LianXiActivity.class);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_fabu:
              //  UItils.startActivity(WrongActivity.class);
                UItils.startActivity(OneHotActivity.class);
                break;
            case R.id.ll_hot:
                UItils.startActivity(CollectActivity.class);
                break;
            case R.id.ll_new:
                UItils.startActivity(SecordActivity.class);
                break;

            default:
        }
    }
}
